package cn.itcast.search.mq;

import cn.itcast.feign.client.ItemClient;
import cn.itcast.hmall.constants.MqConstants;
import cn.itcast.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 监听业务
 */
@Component
@Slf4j
public class MqItemListener {
    @Autowired
    private SearchService searchService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 监听商品新增修改
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(MqConstants.ITEM_INSERT_QUEUE),
            exchange = @Exchange(value = MqConstants.ITEM_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.ITEM_INSERT_KEY))
    public void listenItemInsertOrUpdate(Long id){
        searchService.insertById(id);
        log.info("新增数据成功:{}",id);
    }
    /**
     * 监听商品删除
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(MqConstants.ITEM_DELETE_QUEUE),
            exchange = @Exchange(value = MqConstants.ITEM_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.ITEM_DELETE_KEY))
    public void listenItemDelete(Long id){
        searchService.deleteById(id);
        log.info("删除成功:{}",id);
    }

    /**
     * 监听商品上架
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(MqConstants.ITEM_UP_QUEUE),
            exchange = @Exchange(value = MqConstants.ITEM_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.ITEM_UP_KEY))
    public void listenItemUp(Long id){
        searchService.upById(id);
        log.info("上架了一条数据:{}",id);
    }
    /**
     * 监听商品下架
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(MqConstants.ITEM_DOWN_QUEUE),
            exchange = @Exchange(value = MqConstants.ITEM_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.ITEM_DOWN_KEY))
    public void listenItemDown(Long id){
        searchService.downById(id);
        log.info("下架了一条数据:{}",id);
    }
}
